package com.example.visitsweb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.visitsweb.dto.VisitsRcodeDto;
import com.example.visitsweb.entity.Verify;
import com.example.visitsweb.entity.VisitsRecord;
import com.example.visitsweb.lang.Result;
import com.example.visitsweb.service.VerifyService;
import com.example.visitsweb.service.VisitsRecordService;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MarkerHub
 * @since 2022-04-26
 */
@RestController
@CrossOrigin
@RequestMapping("/file")
public class VisitsRecordController {

    @Autowired
    VisitsRecordService visitsRecordService;
    @Autowired
    VerifyService verifyService;
    /**
     * 来访登记
     *
     * @param visitsRecordDto 来访个人信息
     * @param
     */
    @PostMapping(value = "/upload", produces = "application/json;charset=UTF-8")
    public Result record(VisitsRcodeDto visitsRecordDto, HttpServletRequest request) {
        MultipartFile file = null;
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart){
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
            file = multipartRequest.getFile("file");
        }
        return visitsRecordService.insetRecord(visitsRecordDto, file);
    }
    @GetMapping("/select")
    public Result Record(@RequestParam("phone") String phone,@RequestParam("code") String code){
        System.out.println(phone);System.out.println(code);
    if(!verifyService.Record(phone,code))
    {
      return Result.fail("您查询的信访记录不存在");
    }
    return Result.succ(visitsRecordService.getOne(new QueryWrapper<VisitsRecord>().eq("phone",phone).eq("verify_code",code)));
    }
}
